package org.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.reggie.common.R;
import org.example.reggie.pojo.Employee;
import org.example.reggie.pojo.Student;
import org.example.reggie.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping("/login")
    public R<Student> login(HttpServletRequest request, @RequestBody Student student){
        String password = student.getPassword();

        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<Student>();
        queryWrapper.eq(Student::getName,student.getName());

        Student student1 = studentService.getOne(queryWrapper);
        if (student1 == null){
            return R.error("登陆失败");
        }

        if(!student1.getPassword().equals(password)){
            return R.error("密码错误");
        }

        if (student1.getStatus() == 0){
            return R.error("账号已禁用");
        }

        request.getSession().setAttribute("student",student1.getId());

        return R.success(student1);
    }
    @PostMapping("/register")
    public  R<String> register(HttpServletRequest request ,@RequestBody Student student){
        studentService.save(student);

        return R.success("添加成功");
    }

    /**
     //     * 分页查询
     //     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper();

        studentService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    @PutMapping()
    public R<String> update(HttpServletRequest request,@RequestBody Student student){


        studentService.updateById(student);

        return R.success("学生信息修改成功");
    }

}
